package cn.myfirefly.moming.common.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.myfirefly.moming.common.dao.mapper.DepartmentMapper;
import cn.myfirefly.moming.common.dao.mapper.StaffMapper;
import cn.myfirefly.moming.common.entity.MMDepartment;
import cn.myfirefly.moming.common.model.StaffDto;
import cn.myfirefly.moming.common.model.StaffFilterDto;
import cn.myfirefly.core.BaseDao;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.mybatis.BjuiDataGridFilter;
import cn.myfirefly.core.mybatis.BjuiDataGridSqlHelper;
import cn.myfirefly.utils.CollectionUtil;

@Repository
public class DepartmentDao extends BaseDao<MMDepartment>{
	
	@Autowired
	private StaffMapper staffMapper;
	
	@Autowired
	private DepartmentMapper departmentMapper;
	
	@SuppressWarnings("unchecked")
	public List<MMDepartment> getAll() {
		Session session = getCurrentSession();
		StringBuffer querySql = new StringBuffer("from MMDepartment r where 1=1 order by showIndex ");
		Query query = session.createQuery(querySql.toString());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public MMDepartment getByNo(String departmentNo) {
		Session session = getCurrentSession();
		StringBuffer querySql = new StringBuffer("from MMDepartment t where t.departmentNo=? ");
		Query query = session.createQuery(querySql.toString());
		query.setString(0, departmentNo);
		MMDepartment depart = null;
		List<MMDepartment> list = query.list();
		if (list != null && list.size() > 0) {
			depart = list.get(0);
		}
		return depart;
	}
	
	@SuppressWarnings("unchecked")
	public List<MMDepartment> getChildren(Integer departmentSn) {
		Session session = getCurrentSession();
		Query query = session.createQuery("from MMDepartment t where t.parentDepartSn=? ");
		query.setInteger(0, departmentSn);
		return query.list();
	}
	
	public PageResultDto<StaffDto> getStaffList(StaffFilterDto dto, Integer departmentSn) {
		PageResultDto<StaffDto> resultDto = new PageResultDto<>();
		// 参数处理
		Map<String, Object> paraMap = new HashMap<>();
		paraMap.put("deptSn", departmentSn);
		BjuiDataGridFilter bjuiDataGridFilter = BjuiDataGridSqlHelper.addFilter(dto);
		paraMap.putAll(bjuiDataGridFilter.getParam());
		//分页处理
		Page<StaffDto> page = PageHelper.startPage(dto.getPageCurrent(), dto.getPageSize(), true);
		List<StaffDto> list = staffMapper.getListByDept(paraMap);
		resultDto.setPageCurrent(dto.getPageCurrent());
		resultDto.setTotal((int)page.getTotal());
		resultDto.setList(list);
		return resultDto;
	}
	
	public void updatePId(Integer departmentSn, Integer parentDepartSn) {
		Session session = getCurrentSession();
		Query query = session.createQuery("update MMDepartment set parentDepartSn=? where departmentSn=? ");
		query.setInteger(0, parentDepartSn);
		query.setInteger(1, departmentSn);
		query.executeUpdate();
	}
	
	public String getChildrenDeptByStaffSn(Integer staffSn) {
		List<Map<String, Object>> list = departmentMapper.getChildrenDeptByStaffSn(staffSn);
		if (CollectionUtil.isNotEmpty(list)) {
			Map<String, Object> map = (Map<String, Object>)list.get(0);
			if (map != null) {
				Object obj = map.get("chdDept");
				if (obj != null) {
					return obj.toString();
				}
			}
		}
		return null;
	}
}
